package com.nc.delivery_project.client.add_schedule;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.core.client.GWT;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.nc.delivery_project.client.ClientFactory;
import com.nc.delivery_project.client.request.ScheduleRequest;
import com.nc.delivery_project.shared.proxy.CurrencyProxy;
import com.nc.delivery_project.shared.proxy.DepartmentProxy;
import com.nc.delivery_project.shared.proxy.LegalPersonProxy;
import com.nc.delivery_project.shared.proxy.ScheduleProxy;
import java.sql.Date;
import java.util.List;


/**
 * Created by DRAGON on 17.01.2015.
 */
public class AddScheduleActivity extends AbstractActivity implements AddSchedulePresenter {

    private AddScheduleView view;

    private ClientFactory clientFactory;

    private List<DepartmentProxy> departments;
    private List<LegalPersonProxy> legalpersons;
    private List<CurrencyProxy> currencys;

    public AddScheduleActivity(Place place, ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
    }

    @Override
    public void start(AcceptsOneWidget panel, EventBus eventBus) {
        if (view == null)
            view = new AddScheduleView();

        view.setPresenter(this);

        panel.setWidget(view);

        fetchDepartments();
        fetchLegalPersons();
        fetchCurrencys();
    }

    @Override
    public void goTo(Place place) {
        clientFactory.getPlaceController().goTo(place);
    }

    @Override
    public ClientFactory getClientFactory() {
        return clientFactory;
    }

    @Override
    public void addSchedule()  {
        ScheduleRequest request = clientFactory.getRequestFactory().createScheduleRequest();

        ScheduleProxy schedule = request.create(ScheduleProxy.class);
        schedule.setOrigin(departments.get(view.originLstBox.getSelectedIndex()));
        schedule.setDestination(departments.get(view.destinationLstBox.getSelectedIndex()));
        schedule.setPrimeCost(Double.parseDouble(view.primeCostTxtBox.getText()));
        schedule.setSellingRate(Double.parseDouble(view.sellingRateTxtBox.getText()));
        schedule.setDepartureDate(Date.valueOf(view.departureDateTxtBox.getText()));
        schedule.setArrivalDate(Date.valueOf(view.arrivalDateTxtBox.getText()));
        schedule.setTariffDate(Date.valueOf(view.tariffDateTxtBox.getText()));
        schedule.setTransportCompany(legalpersons.get(view.transportCompanyLstBox.getSelectedIndex()));
        schedule.setCurrency(currencys.get(view.currencyLstBox.getSelectedIndex()));
        request.create(schedule).fire();
    }
    @Override
    public void fillDepartmentDropBox(List<DepartmentProxy> departments) {
        for (DepartmentProxy department : departments) {
            view.originLstBox.addItem(department.getAddress(), String.valueOf(department.getDeptId()));
            view.destinationLstBox.addItem(department.getAddress(), String.valueOf(department.getDeptId()));
        }
    }
    @Override
    public void fillLegalPersonDropBox(List<LegalPersonProxy> legalpersons) {
        for (LegalPersonProxy legalperson : legalpersons) {
            view.transportCompanyLstBox.addItem(legalperson.getTitle(), String.valueOf(legalperson.getLegalPersonId()));
        }
    }
    @Override
    public void fillCurrencyDropBox(List<CurrencyProxy> currencys) {
        for (CurrencyProxy currency : currencys) {
            view.currencyLstBox.addItem(currency.getTitle(), String.valueOf(currency.getId()));
        }
    }
    private void fetchDepartments() {
        clientFactory.getRequestFactory().createDepartmentRequest().read().fire(new Receiver<List<DepartmentProxy>>() {
            @Override
            public void onSuccess(List<DepartmentProxy> response) {
                GWT.log("origin,destination fetched");
                departments = response;

                fillDepartmentDropBox(departments);
            }
        });
    }
    private void fetchLegalPersons() {
        clientFactory.getRequestFactory().createLegalPersonRequest().read().fire(new Receiver<List<LegalPersonProxy>>() {
            @Override
            public void onSuccess(List<LegalPersonProxy> response) {
                GWT.log("transportCompany fetched");
                legalpersons = response;

                fillLegalPersonDropBox(legalpersons);
            }
        });
    }
    private void fetchCurrencys() {
        clientFactory.getRequestFactory().createCurrencyRequest().read().fire(new Receiver<List<CurrencyProxy>>() {
            @Override
            public void onSuccess(List<CurrencyProxy> response) {
                GWT.log("transportCompany fetched");
                currencys = response;

                fillCurrencyDropBox(currencys);
            }
        });
    }
}
